<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-'>/**
</span> * @ignore
 * view for kissy mvc : event delegation,el generator
 * @author yiminghe@gmail.com
 */
KISSY.add(&quot;mvc/view&quot;, function (S, Node, Base) {

    var $ = Node.all;

    function normFn(self, f) {
        if (typeof f == 'string') {
            return self[f];
        }
        return f;
    }

<span id='KISSY-MVC-View'>    /**
</span>     * View for delegating event on root element.
     * @class KISSY.MVC.View
     * @extends KISSY.Base
     */
    return Base.extend({
        initializer: function () {
            var events;
            if (events = this.get(&quot;events&quot;)) {
                this._afterEventsChange({
                    newVal: events
                });
            }
        },

        _afterEventsChange: function (e) {
            var prevVal = e.prevVal;
            if (prevVal) {
                this._removeEvents(prevVal);
            }
            this._addEvents(e.newVal);
        },

        _removeEvents: function (events) {
            var el = this.get(&quot;el&quot;);
            for (var selector in events) {
                var event = events[selector];
                for (var type in event) {
                    var callback = normFn(this, event[type]);
                    el.undelegate(type, selector, callback, this);
                }
            }
        },

        _addEvents: function (events) {
            var el = this.get(&quot;el&quot;);
            for (var selector in events) {
                var event = events[selector];
                for (var type in event) {
                    var callback = normFn(this, event[type]);
                    el.delegate(type, selector, callback, this);
                }
            }
        },

<span id='KISSY-MVC-View-method-render'>        /**
</span>         * @chainable
         */
        render: function () {
            return this;
        },

<span id='KISSY-MVC-View-method-destroy'>        /**
</span>         * Remove root element.
         */
        destroy: function () {
            this.get(&quot;el&quot;).remove();
        }

    }, {
        ATTRS: {
<span id='KISSY-MVC-View-property-el'>            /**
</span>             * Get root element for current view instance.
             * @type {String}
             *
             *
             *      //  selector :
             *      .xx
             *      // or html string
             *      &lt;div&gt;my&lt;/div&gt;
             */
            el: {
                value: &quot;&lt;div /&gt;&quot;,
                getter: function (s) {
                    if (typeof s == 'string') {
                        s = $(s);
                        this.setInternal(&quot;el&quot;, s);
                    }
                    return s;
                }
            },

<span id='KISSY-MVC-View-property-events'>            /**
</span>             * Delegate event on root element.
             * @type {Object}
             *
             *
             *      selector:{
             *          eventType:callback
             *      }
             */
            events: {

            }
        }
    });
}, {
    requires: ['node', 'base']
});</pre>
</body>
</html>
